package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.ProcessStaMapper;
import com.indusfo.spc.pojo.ProcessSta;
import com.indusfo.spc.pojo.Station;
import com.indusfo.spc.service.ProcessStaService;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author louk
 * @date 2019/9/2 10:39
 */
@Service
public class ProcessStaServiceImpl  implements ProcessStaService {
    private static final Logger logger = LoggerFactory.getLogger(ProcessStaServiceImpl.class);
    @Resource
    private ProcessStaMapper processStaMapper;


    /**
     * 分页查询 pagesize pageindex
     * 模糊查询 dim
     * ID查询  Pid
     * 返回总数 count
     * @param processSta
     * @return
     */
    @Override
    public JSONObject queryProcessSta(ProcessSta processSta) {
        try {
            //获取页面的数据个数
            Integer pagesize = processSta.getPagesize();
            //获取查询的页数
            Integer pageindex = processSta.getPageindex();
            if(pagesize!=null && pageindex !=null){
                processSta.setIncept(pagesize*(pageindex-1));
            }
            List<ProcessSta> listProcessBom=processStaMapper.queryProcessSta(processSta);
            if(listProcessBom.isEmpty()){
                return  JSONObject.oK("没有查询到相关消息",listProcessBom,0);
            }
            //如果页数和页面数量都不为空的情况下进行计数

            Integer count= processStaMapper.countProcessSta(processSta);
            return JSONObject.oK("查询成功",listProcessBom,count);
        } catch (GlobalException e) {
            // 捕获异常,打印并返回数
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());

        }
    }

    /**
     * 非空判断 长度验证   checkParam
     * 查重判断 checkParamsNotRepeat
     * 默认值赋值 setDataState(1)
     * @param processSta
     * @return
     */
    @Override
    public JSONObject insertProcessSta(ProcessSta processSta) {
        /**
         * 这里还要判断哪些不为空
         */
        try{
            if(processSta.getProcessStaId()!=null){
                throw new ModifyFailedException("新增时流程工位ID不能存在");
            }
            if(processSta.getDataState()==null){
                processSta.setDataState(1);
            }
            checkParam(processSta);
            Integer row=processStaMapper.insertProcessSta(processSta);
            if(row==0){
                throw new ModifyFailedException("新增流程工位失败");
            }
            return JSONObject.oK("新增成功");
        }catch(GlobalException e) {//捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 多数据删除 Long[] bldcIds
     * 级联删除 delteBldcByBldcTypeId
     * @param processStaIds
     * @param IDataState
     * @return
     */
    @Override
    public JSONObject deleteProcessSta(List<Integer> processStaIds, Integer IDataState) {
        try{
            //判断传入的参数是否为null
            if(IDataState == null) {
                //为null抛出异常
                throw new ParamsErrorException("数据状态不能为空");
            }else if(IDataState != 1 && IDataState != 2 && IDataState != 3) {
                throw new ParamsErrorException("数据状态错误");
            }
            if(processStaIds == null) {
                throw new ParamsErrorException("请选择设备资料");
            }
            //调用删除的接口
            int row = processStaMapper.deleteProcessSta(processStaIds,IDataState);
            //声明msg字符串，用来存抛出的异常
            String msg = "";
            if (row == 0) {
                // 判断传入的数据状态参数lDataState,返回相应信息
                switch (IDataState) {
                    case 1:
                        throw new ModifyFailedException("启用失败");
                    case 2:
                        throw new ModifyFailedException("删除失败");
                    case 3:
                        throw new ModifyFailedException("停用失败");
                    default:
                        break;
                }
            } else {
                switch (IDataState) {
                    case 1:
                        msg = "启用成功";break;
                    case 2:
                        msg = "删除成功";break;
                    case 3:
                        msg = "停用成功";break;
                    default:
                        break;
                }
            }
            return JSONObject.oK(msg);
        }catch(GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 非空判断 checkParam
     * 查重判断 checkParamsNotRepeat
     * 默认值赋值 setDataState(1)
     * @param processSta
     * @return
     */
    @Override
    public JSONObject updateProcessSta(ProcessSta processSta) {
        try {
            //判断传入的参数是否为null
            if(processSta.getProcessStaId()==null) {
                //为null则抛出异常
                throw new ParamsErrorException("修改时流程工位id不能为空");
            }
            if(processSta.getDataState()==null){
                processSta.setDataState(1);
            }
            checkParam(processSta);

            //调用修改的接口
            int row = processStaMapper.updateProcessSta(processSta);
            if(row == 0) {
                throw new ModifyFailedException("数据修改失败");
            }
            return JSONObject.oK("数据修改成功");
        }catch (GlobalException e) {//捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    @Override
    public JSONObject queryStation(ProcessSta processSta) {
        List<Station> liststStations = null;
        try {
            Integer pagesize = processSta.getPagesize();
            Integer pageindex = processSta.getPageindex();
            if(pagesize != null && pageindex != null) {
                processSta.setIncept(pagesize*(pageindex - 1));
            }

            liststStations = processStaMapper.listStation(processSta);
            if (liststStations.isEmpty()) {
                return JSONObject.oK("没有产线相关数据", liststStations, 0);
            }
            //查询分页总记录数
            int count = processStaMapper.countStation(processSta);
            return JSONObject.oK("查询成功", liststStations, count);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    @Override
    public JSONObject changeStation(List<Integer> StaIds, Integer lProFlow) {
        try {
            //空值判断
            if(lProFlow==null){
                throw new ParamsErrorException("请选择工序流程！");
            }
            //删除该流程下所有数据
            processStaMapper.deleteByIProFlow(lProFlow);
            //新增新数据
            if(StaIds.size()!=0){
                for(int i=0;i<StaIds.size();i++){
                    ProcessSta processSta=new ProcessSta();
                    processSta.setlProFlow(lProFlow);
                    processSta.setStaId(StaIds.get(i));
                    processSta.setDataState(1);
                    processStaMapper.insertProcessSta(processSta);
                }
            }



            return JSONObject.oK("移动成功");
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    public void checkParam(ProcessSta processSta){
//        if(processSta.getStaId()==null){
//            throw new ParamsErrorException("流程工位ID不能为空");
//        }
        if(processSta.getlProFlow()==null){
            throw new ParamsErrorException("工艺流程ID不能为空");
        }
        if(processSta.getStaId()==null){
            throw new ParamsErrorException("工位ID不能为空");
        }
        // 名称不能重复
        Integer Counts = processStaMapper.checkProcessSta(processSta);
        if (Counts !=0) {
            throw new ParamsErrorException("该工艺流程已存在");
        }
    }
}
